﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace ProjectCastamere
{
    /// <summary>
    /// This class holds all the attributes for the Player.
    /// </summary>
    class Player
    {
        /// <summary>
        /// The following code initialises the Get/Set methods for the Players stats etc
        /// </summary>

        // The Player's Speed
        private float speed;

        public float Speed
        {
            get { return speed; }
            set { speed = value; }
        }

        // The Player's Jumping Height
        private float jumpHeight;

        public float JumpHeight
        {
            get { return jumpHeight; }
            set { jumpHeight = value; }
        }

        // The Player's 3D position
        private Vector3 position = new Vector3(19, 10, 19);

        public Vector3 Position
        {
            get { return position; }
            set { position = value; }
        }

        // The experience of the player
        private int experience;

        public int Experience
        {
            get { return experience; }
            set { experience = value; }
        }

        // The number of skill points a user has
        private int skillPoints;

        public int SkillPoints
        {
            get { return skillPoints; }
            set { skillPoints = value; }
        }

        /// <summary>
        /// The following code initalises the weapons, camera and Bank account of the player
        /// </summary>
        
        // Initialise the number of weapons( temp var, might change in the future )
        private const int numberOfWeapons = 7;

        // Initialise the weapons
        private Weapon[] weapons = new Weapon[numberOfWeapons];

        // Camera
        private Camera camera;

        public Camera Camera
        {
            get { return camera; }
            set { camera = value; }
        }

        // Player Bank
        private Bank bank;

        // Skills the player has aquired throughout the game session
        private List<Skill> skills = new List<Skill>();

        // players rotation about the y(up) axis
        private float yRotation = 225.0f;

        // players rotation about the x axis
        private float xRotation = 90.0f;

        private float zRotation = 0.0f;

        // Player Constructor
        public Player(float aspectRatio)
        {
            // Initalise a camera
            camera = new Camera();

            // Initialise a new bank, with a cash amount
            bank = new Bank(2000);

            // Initialise the weapons
            for (int w = 0; w < numberOfWeapons; w++)
            {
                weapons[w] = null;// new Weapon(); player unlocks weapons
            }

            camera.ProjectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(60.0f), aspectRatio, 0.1f, 100.0f);
        }

        // Retrieve the Player skill
        public Skill GetSkill(string name)
        {
            Skill skill = new Skill();
            return skill;
        }

        /// <summary>
        /// calculates the players view direction based off of their x and y rotation
        /// </summary>
        /// <returns></returns>
        public Vector3 CalculateDirectionVector()
        {
            // calculate the new direction vector based on x and y rotation
            Vector3 xzDir = new Vector3(
                    (float)Math.Sin(MathHelper.ToRadians(yRotation)),
                    0.0f,
                    (float)Math.Cos(MathHelper.ToRadians(yRotation)));

            float x = (float)Math.Sin(MathHelper.ToRadians(xRotation));
            float y = (float)Math.Cos(MathHelper.ToRadians(xRotation));

            return (xzDir * x) + (Vector3.Up * y);
        }

        private void EnforceRotationalConstraints()
        {
            xRotation = Math.Max(1.0f, Math.Min(179.0f, xRotation));
        }

        // Retrieve input from the user to control the player
        public void Input()
        {
          //  InputManager.IsActionPressed();
            //xRotation += 0.1f;
            //zRotation += 0.1f;

            EnforceRotationalConstraints();

            // update the camera's view matrix based on player position and view direction
            camera.ViewMatrix = Matrix.CreateLookAt(position, position + CalculateDirectionVector(), Vector3.Up) * Matrix.CreateRotationZ(MathHelper.ToRadians(zRotation));
        }
    }
}
